package com.changgou.service.admin.controller;


import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.service.admin.service.AdminService;
import com.changgou.service.admin.util.JwtUtil;
import com.changgou.system.pojo.TbAdmin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/system")
public class AdminController {

    @Resource
    private AdminService adminService;

    @PostMapping
    public Result<Void> add(@RequestBody TbAdmin tbAdmin){
        adminService.add(tbAdmin);
        return Result.success();
    }

    /**
     * 登录
     * @param tbAdmin
     * @return
     */
    @PostMapping("/admin/login")
    public Result<Map<String, Object>> login(@RequestBody TbAdmin tbAdmin){
        Boolean result = adminService.login(tbAdmin);
        if (result){
            Map<String, Object> adminInfo = new HashMap<>();
            adminInfo.put("username", tbAdmin.getLoginName());
            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), tbAdmin.getLoginName(), null);
            adminInfo.put("token", token);
            return Result.success(adminInfo);
        }
        return Result.error(StatusCode.LOGINERROR,"用户名或密码错误", null);
    }

}
